//
//  LaunchAdView.m
//  DLCashbook
//
//  Created by mac1012 on 2017/6/1.
//  Copyright © 2017年 DianlE. All rights reserved.
//

#import "LaunchAdView.h"
#import "AdvertisingManager.h"
#define WAITTIME 10             //开屏请求等待时间
@interface LaunchAdView()



@end

@implementation LaunchAdView



+ (void)showLaunch:(NSInteger)showTimer withEnterForeground:(BOOL)isEnterForeground WithBlock:(TouchBlock)block{
    LaunchAdView *view = [[LaunchAdView alloc] init];
    view.touchBlock = block;
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window.rootViewController = [[UIViewController alloc] init];
    window.rootViewController.view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
    window.windowLevel = UIWindowLevelStatusBar + 1;
    window.hidden = false;
    window.alpha = 1;
    [window addSubview:view];
    view.myWindow = window;
    view.count = showTimer;
    NSInteger loadTime = isEnterForeground ? 4:showTimer;
    
    [NSNotificationCenter.defaultCenter addObserver:view selector:@selector(nativeDidFinishLoading) name:@"nativeDidFinishLoading" object:nil];
    [NSTimer scheduledTimerWithTimeInterval:loadTime target:view selector:@selector(nativeIsFinishLoading) userInfo:nil repeats:false];
    
   
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
        if(view.countTimer == nil){
            //[view dismissAction];
            [[AdvertisingManager shareAdvertisingManager] recyclePrimaryView];
            [[AdvertisingManager shareAdvertisingManager] loadNative];
            
            if ([[AdvertisingManager shareAdvertisingManager] isShowAdvertising] ){
                [LaunchAdView showLaunch:8 withEnterForeground:true WithBlock:^(NSURL *url) {
                    
                }];
            }
        }
    }];

    
}

- (void)nativeIsFinishLoading{
    if (self.countTimer == nil) {
        if ([AdvertisingManager shareAdvertisingManager].native.isReady) {
            return ;
        }
        [self dismissAction];
    }
}

- (void)nativeDidFinishLoading{
    self.webURL = [AdvertisingManager shareAdvertisingManager].native.adLandingPageUrl;
    [self.countBtn setTitle:[NSString stringWithFormat:@"跳过 %ld",(long)self.count] forState:UIControlStateNormal];
    self.countBtn.hidden = false;
    self.countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.countTimer forMode:NSRunLoopCommonModes];
    
    UIView *adView = [[AdvertisingManager shareAdvertisingManager].native primaryViewOfWidth: [UIScreen mainScreen].bounds.size.width];
    [self addSubview:adView];
    [self bringSubviewToFront:self.countBtn];

}

- (instancetype)init{
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeigt = [UIScreen mainScreen].bounds.size.height;
    if (self = [super initWithFrame:CGRectMake(0, 0, screenWidth, screenHeigt)]) {
        self.backgroundColor =[UIColor colorWithWhite:1 alpha:0.8];
        _adView = [[UIView alloc] init];
        _adView.frame = [self bounds];
        _adView.userInteractionEnabled = true;
        _adView.contentMode = UIViewContentModeScaleAspectFill;
        _adView.clipsToBounds = true;

        [self addSubview:_adView];
        CGFloat btnW = 60;
        CGFloat btnH = 30;
        self.countBtn = [[UIButton  alloc] init];
        self.countBtn.frame = CGRectMake(screenWidth - btnW - 24 , btnH, btnW, btnH);
        self.countBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.countBtn.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.6];
        self.countBtn.layer.cornerRadius = 4;
        [self.countBtn addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
        self.countBtn.hidden = false;
         [self.countBtn setTitle:[NSString stringWithFormat:@"跳过"] forState:UIControlStateNormal];
        [self addSubview:self.countBtn];
        
    }
    return self;
}

- (void)tapAction{
    if (_touchBlock) {
        _touchBlock(_webURL);
        [self dismissAction];
    }
}

- (void)countDown{
    --self.count;
    [self.countBtn setTitle:[NSString stringWithFormat:@"跳过 %ld",_count] forState:UIControlStateNormal];
    if (self.count == 0) {
        [self dismissAction];
    }

}

- (void)dismissAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    if (self.countTimer != nil) {
        [self.countTimer invalidate];
        self.countTimer = nil;
    }
    self.myWindow = nil;
    
}

@end
